# 使用 requests
import requests

import app


class EgoApi():

    def __init__(self):
        self.banner_url = app.BASE_URL + "/api/v1/banner/1"
        self.theme_url = app.BASE_URL + "/api/v1/theme"
        self.recent_product_url = app.BASE_URL + "/api/v1/product/recent"
        self.category_url = app.BASE_URL + "/api/v1/category/all"
        self.category_product_url = app.BASE_URL + "/api/v1/product/by_category"
        self.product_url = app.BASE_URL + "/api/v1/product/2"
        self.token_url = app.BASE_URL + "/api/v1/token/user"
        self.verify_token_url = app.BASE_URL + "/api/v1/token/verify"
        self.address_url = app.BASE_URL + "/api/v1/address"
        self.order_user_url = app.BASE_URL + "/api/v1/order/by_user"
        self.create_order_url = app.BASE_URL + "/api/v1/order"
        self.query_order_url = app.BASE_URL + "/api/v1/order"

    # 1 获取轮播图
    def t1_get_banner(self):
        return requests.get(url=self.banner_url)

    # 2 获取专题栏位
    def t2_get_theme(self, ids):
        # 写法1  url =self.theme_url + "?" + ids
        # 写法2  return requests.get(url=self.theme_url, params=ids)
        # 写法3
        return requests.get(url=self.theme_url, params={"ids": ids})

    # 3 获取最近新品
    def t3_get_recent_product(self):
        return requests.get(url=self.recent_product_url)

    # 4 获取商品分类
    def t4_get_category(self):
        return requests.get(url=self.category_url)

    # 5 获取商品分类下的商品
    def t5_get_category_product(self, id):
        return requests.get(url=self.category_product_url, params=id)

    # 6 获取商品详情信息
    def t6_get_product(self):
        return requests.get(url=self.product_url)

    # 7 获取 token
    def t7_get_token(self, json):
        return requests.post(url=self.token_url, json=json)

    # 8 token验证 写法1-2
    # def t8_verify_token(self, token):
    #     return requests.post(url=self.verify_token_url, json={"token": token})

    def t8_verify_token(self, headers, json):
        return requests.post(url=self.verify_token_url, headers=headers, json=json)

    # 9 获取地址信息
    def t9_get_address(self, headers):
        return requests.get(url=self.address_url, headers=headers)

    # 10 获取用户订单列表
    def t10_get_order_user(self, page, headers):
        return requests.get(url=self.order_user_url, params=page, headers=headers)

    # 11 创建订单
    def t11_create_oder(self, headers, json):
        return requests.post(url=self.create_order_url, headers=headers, json=json)

    # 12 查看订单
    def t12_query_oder(self, headers, order_id):
        query_order_url = self.query_order_url + "/" + order_id
        return requests.get(url=query_order_url, headers=headers)


if __name__ == '__main__':
    ego_api = EgoApi()
    response1 = ego_api.t1_get_banner()
    # response1 = ego_api.t1_get_theme("ids=1,2,3")
    response2 = ego_api.t2_get_theme("1,2,3")
    response3 = ego_api.t3_get_recent_product()
    response4 = ego_api.t4_get_category()
    response5 = ego_api.t5_get_category_product("id=2")
    response6 = ego_api.t6_get_product()
    response7 = ego_api.t7_get_token({"code": app.CODE})
    response8 = ego_api.t8_verify_token(app.HEADERS, {"token": app.TOKEN})

    print("更新前的headers：", app.HEADERS)
    app.HEADERS["token"] = "fa6082ecdfad444e2a6995c6ea2df7c8"
    print("更新后的headers：", app.HEADERS)
    response9 = ego_api.t9_get_address(app.HEADERS)

    app.HEADERS["token"] = "fa6082ecdfad444e2a6995c6ea2df7c8"
    response10 = ego_api.t10_get_order_user("page=1", app.HEADERS)

    app.HEADERS["token"] = "fa6082ecdfad444e2a6995c6ea2df7c8"
    response11 = ego_api.t11_create_oder(app.HEADERS,
                                         {"products": [{"product_id": 8, "count": 1}, {"product_id": 10, "count": 2}]})

    app.HEADERS["token"] = "fa6082ecdfad444e2a6995c6ea2df7c8"
    response12 = ego_api.t12_query_oder(app.HEADERS, "46")

    print(response12.json())
